package com.ui.fragment;

import android.annotation.SuppressLint;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;

import com.base.ex.ExBaseFragment;
import com.ui.contract.FrameContract;
import com.ui.main.R;
import com.ui.model.FrameModel;
import com.ui.presenter.FramePresenter;

import butterknife.Bind;

/**
 * @Description
 * @Author luxiao418
 * @Email luxiao418@pingan.com.cn
 * @Date 16/8/12 11:13
 * @Version v2.6.0
 */
public class FrameFragment extends ExBaseFragment<FramePresenter, FrameModel> implements FrameContract.View {
    @Bind(R.id.tabhost)
    FragmentTabHost mTabHost;
    private int[] mImages;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_frame;
    }

    @Override
    protected void initView() {
        mTabHost.setup(mContext.getApplicationContext(), getChildFragmentManager(), R.id.tabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);
        mTabHost.setCurrentTab(0);  //默认选择第一个
    }

    @Override
    public void showTabList(String[] tabs, int[] images) {
        mImages = images;
        for (int i = 0; i < tabs.length; i++) {
            // Tab按钮添加文字和图片
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tabs[i]).setIndicator(getImageView(i));
            // 添加Fragment
            mTabHost.addTab(tabSpec, FragmentFactory.get(tabs[i]).getClass(), null);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.pedo_actionbar_bkg);
            mTabHost.setTag(i);
        }
    }

    // 获得图片资源
    private View getImageView(int index) {
        @SuppressLint("InflateParams")
        View view = mContext.getLayoutInflater().inflate(R.layout.view_tab_indicator, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tab_iv_image);
        imageView.setImageResource(mImages[index]);
        return view;
    }
}
